#include Adafruit_NeoPixel.h>

// Configuración del NeoPixel
#define PIXEL_PIN    27  // Cambia esto al pin donde conectaste el DIN del anillo
#define NUM_PIXELS   1  // Cambia esto al número de LEDs que tiene tu anillo
Adafruit_NeoPixel ring(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

const int pwmInputPin = 28;
const unsigned long PERIOD_US = 20000UL; 

// Matriz de colores: 11 posiciones (0 al 10), cada una con {R, G, B}
int colors[11][3] = {
  {255, 0, 0},   // 0: Rojo
  {0, 255, 0},   // 1: Verde
  {0, 0, 255},   // 2: Azul
  {255, 255, 0}, // 3: Amarillo
  {0, 255, 255}, // 4: Cian
  {255, 0, 255}, // 5: Magenta
  {255, 165, 0}, // 6: Naranja
  {128, 0, 128}, // 7: Púrpura
  {255, 255, 255},// 8: Blanco
  {100, 100, 100},// 9: Gris
  {50, 200, 100}  // 10: Turquesa
};

void setup() {
  Serial.begin(115200);
  pinMode(pwmInputPin, INPUT);
  
  ring.begin();           // Inicializa el anillo
  ring.setBrightness(50); // Brillo al 20% para no deslumbrar (0-255)
  ring.show();            // Apaga todos los LEDs al inicio
}

void loop() {
  unsigned long highTime = pulseIn(pwmInputPin, HIGH, 100000UL); 

  if (highTime == 0) {
    Serial.println("Sin señal o timeout");
    delay(200);
    return;
  }

  float dutyCycle = (float)highTime / PERIOD_US;
  float porcentaje = dutyCycle * 100.0f;
  
  // Decodificar el número (0-10)
  int numero = round((porcentaje - 5.0f) / 9.0f);
  numero = constrain(numero, 0, 10); 

  Serial.print("Num recibido: ");
  Serial.print(numero);
  
  // --- Lógica del NeoPixel ---
  // Extraemos los valores de la matriz según el número recibido
  int r = colors[numero][0];
  int g = colors[numero][1];
  int b = colors[numero][2];

  Serial.printf(" -> Color RGB: [%d, %d, %d]\n", r, g, b);

  // Aplicar el color a todos los LEDs del anillo
  for(int i = 0; i < NUM_PIXELS; i++) {
    ring.setPixelColor(i, ring.Color(r, g, b));
  }
  ring.show(); // Actualiza el anillo físicamente

  delay(100);
}